﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using BCCommon;
using BCEntity.WeChatComponent.WeChatNewsImageRecord;
using Dapper;

namespace BCData.WeChatComponent.WeChatNewsImageRecord
{
    /// <summary>
    ///  微信图文内部图片记录数据处理
    /// </summary>
    public class WeChatNewsImageRecordData : IWeChatNewsImageRecordData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY = @"SELECT A.WeChatNewsImageRecordId,A.UploadFileRecordNo,A.Url,A.CreateTime,
                                              B.FilePath,B.FileSuffix,B.WebsiteFullName
                                        FROM WeChatNewsImageRecord A 
                                            INNER JOIN UploadFileRecord B
                                                ON A.UploadFileRecordNo=B.UploadFileRecordNo";
        public WeChatNewsImageRecordData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public WeChatNewsImageRecordViewEntity Add(WeChatNewsImageRecordEntity weChatNewsImageRecordEntity)
        {
            string sql = @"INSERT INTO WeChatNewsImageRecord(UploadFileRecordNo,Url)VALUES(?UploadFileRecordNo,?Url);
                            {0} WHERE A.WeChatNewsImageRecordId=LAST_INSERT_ID();";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", weChatNewsImageRecordEntity.UploadFileRecordNo, DbType.AnsiString, size: 22);
            parameters.Add("?Url", weChatNewsImageRecordEntity.Url, DbType.AnsiString, size: 1000);
            return this.databaseContext.Master.QueryEntity<WeChatNewsImageRecordViewEntity>(string.Format(sql, QUERY), parameters, databaseContext.Transaction);
        }

        public WeChatNewsImageRecordViewEntity Get(string uploadFileRecordNo)
        {
            string sql = @"{0} WHERE A.UploadFileRecordNo=?UploadFileRecordNo;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?UploadFileRecordNo", uploadFileRecordNo, DbType.AnsiString, size: 22);
            return this.databaseContext.Slave.QueryEntity<WeChatNewsImageRecordViewEntity>(string.Format(sql, QUERY), parameters);
        }

        public WeChatNewsImageRecordViewEntity Get(long weChatNewsImageRecordId)
        {
            string sql = @"{0} WHERE A.WeChatNewsImageRecordId=?WeChatNewsImageRecordId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?WeChatNewsImageRecordId", weChatNewsImageRecordId, DbType.Int64, size: 20);
            return this.databaseContext.Slave.QueryEntity<WeChatNewsImageRecordViewEntity>(string.Format(sql, QUERY), parameters);
        }

        public IEnumerable<WeChatNewsImageRecordViewEntity> Get(DateTime? start, DateTime? end, out int dataCount, bool? ascending, int pageIndex = 1, int pageSize = 10)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime {4} LIMIT {2},{3}; 
                               SELECT Count(1) 
                                FROM WeChatNewsImageRecord A 
                                    INNER JOIN UploadFileRecord B
                                        ON A.UploadFileRecordNo=B.UploadFileRecordNo {1}",
                QUERY,
                wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                pageSize,
                ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            var result = this.databaseContext.Slave.QueryMultiple<WeChatNewsImageRecordViewEntity, int>(sql, parameters);

            dataCount = result.Item2;

            return result.Item1;
        }
    }
}
